package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopPayAppSetting;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.ShopPayAppSettingMapper;
import com.yami.shop.service.ShopPayAppSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 店铺支付宝支付设置Service实现类
 */
@Service
@Slf4j
public class ShopPayAppSettingServiceImpl extends ServiceImpl<ShopPayAppSettingMapper, ShopPayAppSetting> implements ShopPayAppSettingService {

    @Resource
    private MessageSource messageSource;

    @Override
    public ShopPayAppSetting getShopPayAppSetting(Long shopId) {
        if (shopId == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        return getById(shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShopPayAppSetting(ShopPayAppSetting shopPayAppSetting) {
        if (shopPayAppSetting.getShopId() == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        
        // 验证提现方式
        if (shopPayAppSetting.getWithdrawalMethod() != null && 
            shopPayAppSetting.getWithdrawalMethod() != 0 && 
            shopPayAppSetting.getWithdrawalMethod() != 1) {
            throw new YamiShopBindException(messageSource.getMessage("withdrawal.method.invalid", null, LocaleContextHolder.getLocale()));
        }
        
        ShopPayAppSetting existSetting = getById(shopPayAppSetting.getShopId());
        if (existSetting == null) {
            // 新增记录
            if (shopPayAppSetting.getWithdrawalMethod() == null) {
                shopPayAppSetting.setWithdrawalMethod(0); // 默认手动提现
            }
            shopPayAppSetting.setCreateTime(LocalDateTime.now());
        }
        
        shopPayAppSetting.setUpdateTime(LocalDateTime.now());
        
        boolean result = saveOrUpdate(shopPayAppSetting);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("shop.pay.appsetting.save.failed", null, LocaleContextHolder.getLocale()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithdrawalMethod(Long shopId, Integer withdrawalMethod) {
        if (shopId == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        
        if (withdrawalMethod == null || (withdrawalMethod != 0 && withdrawalMethod != 1)) {
            throw new YamiShopBindException(messageSource.getMessage("withdrawal.method.invalid", null, LocaleContextHolder.getLocale()));
        }
        
        ShopPayAppSetting shopPayAppSetting = getById(shopId);
        if (shopPayAppSetting == null) {
            // 如果记录不存在，创建新记录
            shopPayAppSetting = new ShopPayAppSetting();
            shopPayAppSetting.setShopId(shopId);
            shopPayAppSetting.setWithdrawalMethod(withdrawalMethod);
            shopPayAppSetting.setCreateTime(LocalDateTime.now());
            shopPayAppSetting.setUpdateTime(LocalDateTime.now());
            
            boolean result = save(shopPayAppSetting);
            if (!result) {
                throw new YamiShopBindException(messageSource.getMessage("shop.pay.appsetting.save.failed", null, LocaleContextHolder.getLocale()));
            }
        } else {
            // 更新现有记录
            shopPayAppSetting.setWithdrawalMethod(withdrawalMethod);
            shopPayAppSetting.setUpdateTime(LocalDateTime.now());
            
            boolean result = updateById(shopPayAppSetting);
            if (!result) {
                throw new YamiShopBindException(messageSource.getMessage("withdrawal.method.update.failed", null, LocaleContextHolder.getLocale()));
            }
        }
    }
} 